<template>
  <div class="live-window-box">
    <div class="live-windows-box-tag" />
    <span class="live-window-title">直播窗口封面</span>
    <div class="live-window-horizontal" />
    <span class="live-window-overview-text">预览：</span>
    <div class="live-window-image">
      <el-upload
        class="avatar-uploader"
        :action="fileUploadAddr"
        :show-file-list="false"
        :on-success="handleUploadPlayImageSuccess"
        :before-upload="beforeAvatarUpload"
        :headers="fileUploadHeaders"
      >
        <img v-if="playImageAddress != ''" :src="playImageAddress" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
    </div>

    <div class="img-desc-text-box">
      <p>
        图片尺寸：865*486像素 图片大小：5M以内；<br>
        图片格式：PNG、JPG、JPEG；<br>
      </p>
    </div>
    <div class="live-window-button-horizontal" />
    <el-button class="submit-button" type="primary" :loading="submitLoading" @click="updateLiveReq">提交</el-button>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { updateLive } from '@/api/lives'

export default {
  name: 'SubjectSetting',
  data() {
    return {
      submitLoading: false,
      channelType: 'common',
      watchChangedData: {
        inputPageButton: ''
      },
      fileUploadAddr: '',
      // 播放器预览图地址
      playImageAddress: '',
      fileUploadHeaders: {
        Authorization: ''
      },
      updateLiveData: {
        CommonBaseImage: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'choicedLive',
      'token'
    ])
  },
  watch: {
    'choicedLive'(n, o) {
      // 现有图片
      this.playImageAddress = n.MobilePlayImage
    },
    'token'(n, o) {
      this.fileUploadHeaders.Authorization = this.token
    }
  },
  mounted() {
    // 获取图片的地址
    this.fileUploadHeaders.Authorization = this.token
    this.fileUploadAddr = process.env.VUE_APP_BASE_API + '/api/v1/files'
    if (this.choicedLive.id) {
      this.playImageAddress = this.choicedLive.MobilePlayImage
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 5

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      // return isJPG && isLt2M
      return isLt2M
    },
    handleUploadPlayImageSuccess(res, file) {
      if (res.data.length > 0) {
        this.playImageAddress = res.data[0].AccessAddress
        this.updateLiveData.CommonBaseImage = res.data[0].AccessAddress
      }
    },
    updateLiveReq() {
      this.submitLoading = true
      updateLive(this.updateLiveData, this.choicedLive.id).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: '保存成功',
            type: 'success'
          })
        }
      })
      this.submitLoading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.live-window-box {
    width: 1627px;
    height: 770px;
    background: #FFFFFF;
    border-radius: 10px;
    position: absolute;
    top: 13px;
    left: 260px;
}
.live-windows-box-tag {
    width: 4px;
    height: 28px;
    background: #2E9BEA;
    border-radius: 2px;
    position: absolute;
    top: 30px;
    }
.live-window-title {
    width: 145px;
    height: 23px;
    font-size: 24px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #4A5462;
    position: absolute;
    top: 31px;
    left: 55px;
}
.live-window-horizontal {
    width: 1514px;
    height: 4px;
    background: #EDF5FB;
    position: absolute;
    top: 85px;
}
.live-window-overview-text {
    width: auto;
    height: 20px;
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #6D7A8E;
    position: absolute;
    top: 139px;
    left: 55px;
}

.live-window-image {
  display: flex;
  position: absolute;
  align-items: center;
  top: 188px;
  left: 56px;
  width: 717px;
  height: 404px;
  ::v-deep {
  .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      margin-left: 20px;
      margin-right: 20px;
      width: 717px;
      height: 404px;
      }
  .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
  .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 717px;
      height: 404px;
      // line-height: 178px;
      text-align: center;
      }
  .avatar {
      width: 717px;
      height: 404px;
      display: block;
    }
  .el-icon-plus {
    align-items: center;
    display: flex;
    justify-content: center;
    }
    }
}
.img-desc-text-box {
    position: absolute;
    width: 466px;
    height: 55px;
    top: 534px;
    left: 850px;
    p {
        line-height: 2rem;
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #8B9DBA;
    }
}
.live-window-button-horizontal {
    width: 1514px;
height: 4px;
background: #EDF5FB;
position: absolute;
top: 645px;
}
.submit-button {
    width: 159px;
    height: 50px;
    background: #2E9BEA;
    border-radius: 10px;
    position: absolute;
    top: 687px;
    left: 659px;
::v-deep {
  span {
    width: 46px;
    height: 20px;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
  }
}
}
</style>
